package com.sdkx.wear_os.utils

import android.content.Context
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.QuickAdapterHelper
import com.chad.library.adapter4.loadState.LoadState
import com.chad.library.adapter4.loadState.trailing.TrailingLoadStateAdapter
import com.sdkx.wear_os.R
import com.sdkx.wear_os.adapter.CustomLoadMoreAdapter

/**
 *根据列表数据返回设置数据
 * @param context 上下文
 * @param listData 列表数据
 * @param helper 用于其他布局，比如加载更多 头布局尾布局等
 * @param page 当前加载第几页  page==0时 没有加载更多功能
 * @param pageSize 每页数量
 * @param isSuccess 是否获取数据成功
 * @param isStateView 是否有空布局功能
 * @param retry 重试
 */
fun <T : Any, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.setAdapterList(
    context: Context,
    listData: List<T> = mutableListOf(),
    helper: QuickAdapterHelper? = null,
    page: Int = 0,
    pageSize: Int = 10,
    isSuccess: Boolean = true,
    isStateView: Boolean = true,
    retry: () -> Unit,
) {
    if (isSuccess) {
        if (page == 0 && listData.isNotEmpty()) {
            submitList(listData)
            helper?.trailingLoadState = LoadState.NotLoading(true)
        } else if ((page == 1 || page == 0) && listData.isEmpty()) {
            //第一页为空
            submitList(null)
            // 打开空布局功能
            isStateViewEnable = isStateView
            //空布局
            setStateViewLayout(context, R.layout.layout_empty)
        } else if (page == 1 && listData.size < pageSize) {
            //第一页数据少于10
            submitList(listData)
            helper?.trailingLoadState = LoadState.NotLoading(true)
        } else if (page > 1 && listData.size < pageSize) {
            //全部加载完成
            addAll(listData)
            //设置状态为未加载，并且没有分页数据
            helper?.trailingLoadState = LoadState.NotLoading(true)
        } else {
            //加载完成
            addAll(listData)
            //设置状态为未加载，并且还有分页数据
            helper?.trailingLoadState = LoadState.NotLoading(false)
        }
    } else {
        if (page == 1 || page == 0) {
            //加载失败并且是第一页
            val failView = LayoutInflater.from(context).inflate(R.layout.layout_load_fail, null)
            //重试
            failView.findViewById<LinearLayout>(R.id.load_fail_data_ll).setOnClickListener {
                retry()
            }
            submitList(null)
            // 打开空布局功能
            isStateViewEnable = isStateView
            //加载失败
            stateView = failView
        } else {
            helper?.trailingLoadState = LoadState.Error(Exception("加载失败"))
        }
    }
}

/**
 * 初始化(用于带有下拉刷新 上拉加载的初始化)
 */
fun <T : Any, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.init(
    swipe: SwipeRefreshLayout,
    onLoad: () -> Unit,
    onFailRetry: () -> Unit,
): QuickAdapterHelper {
    // 自定义"加载更多"的样式
    val loadMoreAdapter = CustomLoadMoreAdapter()
    loadMoreAdapter.setOnLoadMoreListener(object : TrailingLoadStateAdapter.OnTrailingListener {
        override fun onLoad() {
            onLoad()
        }

        override fun onFailRetry() {
            onFailRetry()
        }

        override fun isAllowLoading(): Boolean {
            // 下拉刷新的时候，不允许进行"加载更多"
            return !swipe.isRefreshing
        }
    })
    return QuickAdapterHelper.Builder(this)
        .setTrailingLoadStateAdapter(loadMoreAdapter)
        .build()
}
